package main

import "sort"

type Row struct {
	Name  string
	Age   int
	Score int
}

type Table struct {
	//行
	Rows []Row

	//点击历史：最近点击的列排在前面
	ClickHistory []string
}

func (t Table) Len() int {
	return len(t.Rows)
}

func (t Table) Swap(i, j int) {
	t.Rows[i], t.Rows[j] = t.Rows[j], t.Rows[i]
}

func (t Table) Less(i, j int) bool {
	for _, column := range t.ClickHistory {
		switch column {
		case "Name":
			if t.Rows[i].Name != t.Rows[j].Name {
				return t.Rows[i].Name < t.Rows[j].Name
			}
		case "Age":
			if t.Rows[i].Age != t.Rows[j].Age {
				return t.Rows[i].Age < t.Rows[j].Age
			}
		case "Score":
			if t.Rows[i].Score != t.Rows[j].Score {
				return t.Rows[i].Score < t.Rows[j].Score
			}
		}
	}

	return false
}

func (t *Table) ClickColumn(column string) {

	//移除旧的点击记录
	for i, col := range t.ClickHistory {
		if col == column {
			t.ClickHistory = append(t.ClickHistory[:i], t.ClickHistory[i+1:]...)
			break
		}
	}
}

func (t *Table) Sort() {
	sort.Sort(t)
}
